from typing import Annotated
from fastapi import FastAPI, Form
from fastapi.responses import HTMLResponse

app = FastAPI()


@app.get("/")
async def main():
    html = """\
    <html><body>
        <form action="/message" method="post">
            收信人: <input type="text" name="to"><br>
            内容:<br>
            <textarea name="message" rows="4" cols="50"></textarea><br>
            <input type="submit" value="提交消息">
        </form>
    </body></html>
    """
    return HTMLResponse(content=html, status_code=200)


@app.post("/message")
async def receive_message(to: Annotated[str, Form()], message: Annotated[str, Form()]):
    html = f"""<html><body>
    <div><a href="/">返回</a></div>
    <h4>发信人: [{to}]</h4>
    内容:<br>
    <p>{message}</p>
    </body></html>
    """
    return HTMLResponse(content=html, status_code=200)
